from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from .serializers import AreasSerializer, SubAreasSerializer
from .models import Area
from rest_framework_extensions.cache.mixins import CacheResponseMixin

# Create your views here.


class AreasViewSet(CacheResponseMixin, ReadOnlyModelViewSet):
    """
    省市区信息展示
    """
    # pagination_class = None  # 区划信息不分页

    def get_queryset(self):
        """
        提供数据集:
        获取省市区的信息
        """
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        """获取序列化器类"""
        if self.action == 'list':
            # 序列化省份信息
            return AreasSerializer
        else:
            # 序列化市或区信息
            return SubAreasSerializer

